<?php
//活动表

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;

class OperateModel extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'operate';

    const IS_FINISHED = 1;
    const UNFINISHED = 2;

    const IS_ARCHIVED = 1;
    const UNARCHIVED = 2;

    const FINISHEDS = [
        self::IS_FINISHED => '是',
        self::UNFINISHED => '否',
    ];

    public function deals ()
    {
        return $this->hasMany(DealModel::class, 'operate_id', 'id')->dd();
    }

    // 访问器
    protected function stockEarnings(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function actualEarnings(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function serviceCharge(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function buyMoney(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function stockRate(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function realRate(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }


    protected function totalRate(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    public function strategy ()
    {
        return $this->belongsTo(StrategyModel::class, 'strategy_id', 'id');
    }

    public function note ()
    {
        return $this->hasOne(NoteModel::class, 'operate_id', 'id');
    }
}
